function $(selector){
	return new $.prototype.init(selector);
}

$.prototype = {
		//构造方法
		
		init : function(selector){
//			this.selector = selector;
			if(selector === undefined){
				this.length = 0;
				return this;
			}
			//选择器正则表达式
			var REG = {
					ID : /^#\w+$/,
					CLASS : /^\.\w+$/,
					TAG : /^([a-z]|[A-Z])+$/,
					NAME : /^!\w+$/,
					ATTR : /^(\[\w+=\w+\])+$/,
					POSATTR : /^[#\.!]?\w+(\[\w+=\w+\])+$/,
					EVER : /^\:(first|last)$/,
					POSEVER : /^[#\.!]?\w+\:(first|last)$/,
					FUNC : /^\:(index|text)\(\w+\)$/,
					POSFUNC : /^[#\.!]?\w+\:(index|text)\(\w+\)$/,
					
			};
			var domArray = new Array();
			
			//如果selector是一个Element，则返回Element本身
			if(selector.nodeType == 1){
				this[0] = selector;
				return;
			}else if(typeof selector == "string"){
				var size = selector.split(" ").length;
				
				if(size > 1){
					this.length = 0;
					return this;
				}else{
					
					//如果匹配ID的正则表达式，调用Xigua.MatchFunc.ID
					if(REG.ID.test(selector)){
						this[0]=Xigua.MatchFunc.ID(selector);
						return this;
					}
					//如果匹配CLASS的正则表达式，调用Xigua.MatchFunc.CLASS
					if(REG.CLASS.test(selector)){
						this[0] = Xigua.MatchFunc.CLASS(selector);
						return this;
					}
					//如果匹配TAG的正则表达式，调用Xigua.MatchFunc.TAG
					if(REG.TAG.test(selector)){
						this[0] = Xigua.MatchFunc.TAG(selector);
						return this;
					}
					//如果匹配Name的正则表达式，调用Xigua.MatchFunc.NAME
					if(REG.NAME.test(selector)){
						this[0] = Xigua.MatchFunc.NAME(selector);
						return this;
					}
					//如果匹配Attr的正则表达式，调用Xigua.MatchFunc.ATTR
					if(REG.ATTR.test(selector)){
						this[0] = Xigua.MatchFunc.ATTR(selector);
						return this;
					}
					//如果匹配PosAttr的正则表达式，调用Xigua.MatchFunc.POSATTR
					if(REG.POSATTR.test(selector)){
						this[0] = Xigua.MatchFunc.POSATTR(selector);
						return this;
					}
					//如果匹配EVER的正则表达式，调用Xigua.MatchFunc.EVER
					if(REG.EVER.test(selector)){
						this[0] = Xigua.MatchFunc.EVER(selector);
						return this;
					}					
					//如果匹配POSEVER的正则表达式，调用Xigua.MatchFunc.POSEVER
					if(REG.POSEVER.test(selector)){
						this[0] = Xigua.MatchFunc.POSEVER(selector);
						return this;
					}	
					//如果匹配FUNC的正则表达式，调用Xigua.MatchFunc.FUNC
					if(REG.FUNC.test(selector)){
						this[0] = Xigua.MatchFunc.FUNC(selector);
						return this;
					}	
					//如果匹配POSFUNC的正则表达式，调用Xigua.MatchFunc.POSFUNC
					if(REG.POSFUNC.test(selector)){
						this[0] = Xigua.MatchFunc.POSFUNC(selector);
						return this;
					}	
				}
				
				
				
				
				
				
			}else if(typeof selector == "object"){
				this[0] = selector[0];
				return this;
			}
			this.length=1;
				
				
				
			
		}
		

};

$.fn = $.prototype.init.prototype = $.prototype;

$.extend = $.fn.extend = function(obj) {
	for(var a in obj) {
		this[a] = obj[a];
	}
};
/**
 * $(selector)对象所属方法定义区
 */
$.fn.extend({
	debug : function(){
		console.debug(this[0]);
		return this;
	},
	each : function(callback){
		for(var i = 0 ; i < this[0].length ; i ++){
			var p_this = new $.prototype.init(this[0][i]);
			p_this.p_callback = callback;
			p_this.p_callback(i,p_this);
		}
	},
	find : function(selector){
		return new $.prototype.init(selector);
	},
	index : function(index){
		return  this[0][index];
	},
	hide : function(delay){
		if(delay != 0 || delay != undefined){
			var p_this = this;
			setTimeout(function(){
				if(p_this[0].length == undefined){
					p_this[0].style.display = "none";
				}else{
					for(var i = 0 ; i < p_this[0].length ; i ++){
						p_this[0][i].style.display = "none";
					}
					
				}
				
			}, delay);
		}else{
			if(this[0].length == undefined){
				this[0].style.display = "none";
			}else{
				for(var i = 0 ; i < this[0].length ; i ++){
					this[0][i].style.display = "none";
				}
				
			}
		}
		return this;
	},
	show : function(delay){
		if(delay != 0 || delay != undefined){
			var p_this = this;
			setTimeout(function(){
				if(p_this[0].length == undefined){
					p_this[0].style.display = "inline";
				}else{
					for(var i = 0 ; i < p_this[0].length ; i ++){
						p_this[0][i].style.display = "inline";
					}
					
				}
				
			}, delay);
		}else{
			if(this[0].length == undefined){
				this[0].style.display = "inline";
			}else{
				for(var i = 0 ; i < this[0].length ; i ++){
					this[0][i].style.display = "inline";
				}
				
			}
		}
		return this;
		
		
		
	},
	attr : function(){
		
	},
	html : function(){
		
	},
	text : function(){
		
	},
	val : function(){
		
	},
	addClass : function(){
		
	},
	removeClass : function(){
		
	},
	hasClass : function(){
		
	},
	hasAttr : function(){
		
	},
	removeAttr : function(){
		
	},
	css : function(){
		
	},
	height : function(){
		
	},
	width : function(){
		
	},
	data : function(){
		
	},
	removeData : function(){
		
	},
	click : function(){
		
	},
	change : function(){
		
	},
	blur : function(){
		
	},
	dblclick : function(){
		
	},
	error : function(){
		
	},
	focus : function(){
		
	},
	keydown : function(){
		
	},
	keypress : function(){
		
	},
	keyup : function(){
		
	},
	load : function(){
		
	},
	mousedown : function(){
		
	},
	mousemove : function(){
		
	},
	mouseout : function(){
		
	},
	mouseup : function(){
		
	},
	select : function(){
		
	},
	submit : function(){
		
	},
	append : function(){
		
	},
	after : function(){
		
	},
	appendTo : function(){
		
	},
	before : function(){
		
	},
	clone : function(){
		
	},
	detach : function(){
		
	},
	remove : function(){
		
	},
	prepend : function(){
		
	},
	prependTo : function(){
		
	},
	replaceAll : function(){
		
	},
	replaceWith : function(){
		
	},
	wrap : function(){
		
	},
	size : function(){
		
	},
	add : function(){
		
	},
	next : function(){
		
	},
	nextAll : function(){
		
	},
	parent : function(){
		
	},
	parents : function(){
		
	}
	

});

/**
 * $对象所属方法定义区
 */
$.extend({
	trim : function(){
		
	},
	type : function(){
		
	},
	ajax : function (){
		
	}

});
var Xigua = $.prototype;
Xigua.MatchFunc = {
		ID : function(selector){
			var str = selector.split("#")[1];
			return document.getElementById(str);
		},
		CLASS : function(selector){
			var domArray = new Array();
			var str = selector.split(".")[1];
			var all = document.getElementsByTagName("*");
			for(var i = 0 ; i < all.length ;i++){
				var strarr = all[i].className.split(" ");
				for(var key in strarr){
					if(strarr[key] == str){
						domArray.push(all[i]);
						break;
					}
				}
			}
			return domArray;
		},
		TAG : function (selector){
			return document.getElementsByTagName(selector);
		},
		NAME : function(selector){
			var str = selector.split("!")[1];
			var all = document.getElementsByTagName("*");
			var domArray = new Array();
			for(var i = 0 ; i < all.length ;i++){
				var reg = new RegExp(str);
				if(reg.test(all[i].name)){
					domArray.push(all[i]);
				}
			}
			return domArray;
		},
		ATTR : function(selector){
			var onwer = selector.split("[")[0];
			var reg = /\b\]\[\b/;
			str = selector.replace(onwer,"");
			var strarr = str.split(reg);
			
			strarr[0] = strarr[0].replace("[","");
			strarr[strarr.length-1] = strarr[strarr.length-1].replace("]","");
			
			var all = document.getElementsByTagName("*");
			var domArray = new Array();
			for(var i = 0 ; i < all.length ; i ++){
				var temp = 0;
				for(var j = 0 ; j < strarr.length ; j ++){
					var arr = strarr[j].split("=");
					var key = arr[0];
					var value = arr[1];
					if(all[i].hasAttribute(key) && all[i].getAttribute(key) == value){
						temp++;
					}
				}
				if(temp == strarr.length){
					domArray.push(all[i]);
				}
			}
			return domArray;
		},
		POSATTR : function(selector){
			var onwer = selector.split("[")[0];
			var reg = /\b\]\[\b/;
			str = selector.replace(onwer,"");
			var strarr = str.split(reg);
			
			strarr[0] = strarr[0].replace("[","");
			strarr[strarr.length-1] = strarr[strarr.length-1].replace("]","");
			
			var all = new Array();
			switch (onwer[0]) {
			case "#":{
				all[0] = Xigua.MatchFunc.ID(onwer);
				break;
			}
			case ".":{
				all = Xigua.MatchFunc.CLASS(onwer);
				break;
			}
			case "!":{
				all = Xigua.MatchFunc.NAME(onwer);
				break;
			}							
			default:{
				all = Xigua.MatchFunc.TAG(onwer);
				break;
			}
		}
			var domArray = new Array();
			for(var i = 0 ; i < all.length ; i ++){
				var temp = 0;
				for(var j = 0 ; j < strarr.length ; j ++){
					var arr = strarr[j].split("=");
					var key = arr[0];
					var value = arr[1];
					if(all[i].hasAttribute(key) && all[i].getAttribute(key) == value){
						temp++;
					}
				}
				if(temp == strarr.length){
					domArray.push(all[i]);
				}
			}
			return domArray;
		},
		EVER : function(selector){
			var str = selector.split(":")[1];
			var all = document.getElementsByTagName("*");
			switch (str) {
				case "first":{
					return all[0];
					break;
				}
				case "last":{
					return all[all.length-1];
					break;
				}
			}
		},
		POSEVER : function(selector){
			var onwer = selector.split(":")[0];
			var str = selector.split(":")[1];
			var all = new Array();
			
			switch (onwer[0]) {
				case "#":{
					all[0] = Xigua.MatchFunc.ID(onwer);
					break;
				}
				case ".":{
					all = Xigua.MatchFunc.CLASS(onwer);
					break;
				}
				case "!":{
					all = Xigua.MatchFunc.NAME(onwer);
					break;
				}							
				default:{
					all = Xigua.MatchFunc.TAG(onwer);
					break;
				}
			}
			switch (str) {
				case "first":{
					return all[0];
					break;
				}
				case "last":{
					return all[all.length-1];
					break;
				}
			}
		},
		FUNC : function(selector){
			var str = selector.split(":")[1];
			var onwer = selector.split(":")[0];
			var all = document.getElementsByTagName("*");

			var sarr = str.split("(");
			var funcname = sarr[0];
			var functext = sarr[1].split(")")[0];
			var domArray = new Array();
			switch (funcname) {
				case "index":{
					return all[functext];
					break;
				}
				case "text":{
					for(var i = 0 ; i < all.length ; i ++){
						if(all[i].text == functext){
							domArray.push(all[i]);
						}
					}
					return domArray;
					break;
					
					
				}
			}
		},
		POSFUNC : function(selector){
			var str = selector.split(":")[1];
			var onwer = selector.split(":")[0];
			var all = new Array();
			switch (onwer[0]) {
			case "#":{
				all[0] = Xigua.MatchFunc.ID(onwer);
				break;
			}
			case ".":{
				all = Xigua.MatchFunc.CLASS(onwer);
				break;
			}
			case "!":{
				all = Xigua.MatchFunc.NAME(onwer);
				break;
			}							
			default:{
				all = Xigua.MatchFunc.TAG(onwer);
				break;
			}
		}
			var sarr = str.split("(");
			var funcname = sarr[0];
			var functext = sarr[1].split(")")[0];
			var domArray = new Array();
			switch (funcname) {
				case "index":{
					return all[functext];
					break;
				}
				case "text":{
					for(var i = 0 ; i < all.length ; i ++){
						if(all[i].text == functext){
							domArray.push(all[i]);
						}
					}
					return domArray;
					break;
					
					
				}
			}
		}
		
		
};
